package jms;

import http.Client;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import jaxb.Decodeur;

import transfert.objects.Aller;

public class MetierMessageListener implements MessageListener {
	private String propertyFile = null;
	public  MetierMessageListener(String propertyFile)
	{
		this.propertyFile = propertyFile;
	}
	@Override
	
	public void onMessage(Message message) {
		   try {
	            if (! (message instanceof TextMessage) ) {
	                System.out.println("onMessage() received an unhandled: " + message.getClass().getName());
	                return;
	            }
	            
	            process((TextMessage)message);
	        }
	        catch (Throwable t) {
	            System.err.println((t instanceof JMSException) ? "JMSException" : "Throwable" + " Caught in onMessage(): " + t.getMessage());
	        }
		
	}
	
	 private void process(TextMessage message)throws JMSException {
		 /**
		  * Instancier le decodeur
		  */
		 Decodeur decodeur = Decodeur.getInstance();
		 /*
		  * Transformer XML en Objet o
		  */
		 Object o = decodeur.decode(message.getText());
		 System.out.println(o.toString());
	     Client client = Client.getInstance();
	     client.setPropertyFile(propertyFile);  
	     client.setObject(o);
		 client.appelHttp();
	    }


}
